package com.zsh.webfluxdemo1;

import com.zsh.webfluxdemo1.entity.User;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

/**
 * @author zsh
 * @Description
 * @create 2022-03-08 9:35
 */
public class Client {
    public static void main(String[] args) {
        WebClient webClient = WebClient.create("http://localhost:53056");  //指定调用服务器的地址

        //根据id 查询
        String id = "1";
        User userResult = webClient.get().uri("/user/" + id)
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToMono(User.class)
                .block();

        System.out.println(userResult.getName());

        Flux<User> results = webClient.get().uri("/users")
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToFlux(User.class);

        results.map(stu -> stu.getName())
                .buffer().doOnNext(System.out::println).blockFirst();

    }
}
